/*
 * Copyright (c) 2008 centrevillage <centrevillage@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef  __PROJECTWRITER_H__
#define  __PROJECTWRITER_H__

// std
#include <string>
#include <fstream>
#include <vector>
#include <iterator>

// boost
#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

// seq
#include "def.h"
#include "Event.h"
#include "EventContainer.h"

namespace seq {

class MidiMsg;

class ProjectWriter {
public:
    ProjectWriter();
    bool writeSMF1(const boost::filesystem::path& filepath);
    bool writeRVF(const boost::filesystem::path& filepath);
    bool write(const boost::filesystem::path& filepath);
private:
    //typedef EventContainer<MidiMsg> MsgList;
    //typedef EventContainer<boost::shared_ptr<MetaEvent> > MetaEventList;
    typedef EventContainer<std::vector<BYTE> > BytesList;

    void init();
    void initTrack();

    void writeHeaderChank(WORD format, WORD numOfTracks, WORD tickPer4);
    void writeTrackChank(UINT32 dataSize);
    void writeDataSection(const std::vector<BYTE>& data);
    void writeMsg(std::vector<BYTE>& data, const MidiMsg& msg);
    void insertMsg(size_t trkNo, const EventList::elem_type& elem);
    
    boost::filesystem::ofstream out;
    boost::array<std::vector<BYTE>, 257>   dataList;
    boost::array<BytesList, 257>           bytesLists;
    //boost::array<MsgList, 257>           msgLists;
    //MetaEventList                        metaEvents;
    //EventList                            metaEvents;
};

} // end of namespace seq
#endif   /* ----- #ifndef __PROJECTWRITER_H__  ----- */
